﻿using BasicData.API.Read.Applications.Commands;
using BasicData.API.Read.Dtos;
using BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace BasicData.API.Read.Controllers
{
    /// <summary>
    /// 控制器-读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MangeController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<MangeController> logger;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public MangeController(IMediator mediator, ILogger<MangeController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [AllowAnonymous]
        [HttpPost]
        public  Task<APIResult<UserDTO>> Login(LoginQueryCommand command)
        {
            return  mediator.Send(command);
        }
        /// <summary>
        /// 测试
        /// </summary>
        /// <returns>返回结果</returns>
        [HttpGet]
        public IActionResult GetTest()
        {
            try
            {
                logger.LogInformation("调用了一下测试方法");
                int num = int.Parse("abc");
            }
            catch (Exception ex)
            {
                logger.LogError("调用测试方法报错" + ex.Message);
              
            }
            return Ok("测试");
        }
        /// <summary>
        /// 获取学校数据
        /// </summary>
        /// <returns>返回结果</returns>
        [HttpGet]
        public Task<APIResult<List<ShowDTO>>> GetShool()
        {
            SchoolQueryCommand command = new SchoolQueryCommand();
            return mediator.Send(command);
        }
        /// <summary>
        /// 获取地址
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<List<AddressDTO>>> GetAddress()
        {
            AddressQueryCommand command = new AddressQueryCommand();
            return mediator.Send(command);
        }
        /// <summary>
        /// 我的代办
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返会结果</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<InstanceDTO>>> GetMyPending([FromQuery] MyPendingQueryCommand command)
        {
            command.UserId = User.FindFirstValue("UserId");
            command.RoleId =User.FindFirstValue("RoleId");


            return mediator.Send(command);
        }
        /// <summary>
        /// 查询明细
        /// </summary>
        /// <param name="inforId"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<APIResult<RoomDTO>> GetAppDetail(int? inforId)
        {
            AppDetailQueryCommand command = new AppDetailQueryCommand();
            command.InforId = inforId;
            return mediator.Send(command);
        }
    }
}
